package com.lxw.nonghe.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.entity.ChronicDiseaseCertificate;
import com.lxw.nonghe.entity.Farmer;
import com.lxw.nonghe.mapper.ChronicDiseaseCertificateMapper;
import com.lxw.nonghe.mapper.FarmerMapper;
import com.lxw.nonghe.service.ChronicDiseaseCertificateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChronicDiseaseCertificateServiceImpl extends ServiceImpl<ChronicDiseaseCertificateMapper, ChronicDiseaseCertificate> implements ChronicDiseaseCertificateService {

    @Autowired
    private FarmerMapper farmerMapper;

    @Override
    public Page<ChronicDiseaseCertificate> getChronicDiseaseCertificatePage(int current, int size, String diseaseName) {
        Page<ChronicDiseaseCertificate> page = new Page<>(current, size);
        Page<ChronicDiseaseCertificate> resultPage = (Page<ChronicDiseaseCertificate>) this.baseMapper.selectChronicDiseaseCertificatePage(page, diseaseName);
        fillFarmerName(resultPage.getRecords());
        return resultPage;
    }

    @Override
    public ChronicDiseaseCertificate getChronicDiseaseCertificateByDiseaseName(String diseaseName) {
        ChronicDiseaseCertificate certificate = this.lambdaQuery().eq(ChronicDiseaseCertificate::getDiseaseName, diseaseName).one();
        if (certificate != null && certificate.getFarmerId() != null) {
            Farmer farmer = farmerMapper.selectById(certificate.getFarmerId());
            if (farmer != null) {
                certificate.setFarmerName(farmer.getName());
            }
        }
        return certificate;
    }

    @Override
    public Page<ChronicDiseaseCertificate> queryChronicDiseaseCertificate(
            ChronicDiseaseCertificate queryChronicDiseaseCertificate,
            int current, int size) {

        LambdaQueryWrapper<ChronicDiseaseCertificate> wrapper = new LambdaQueryWrapper<>();

        // 精确查询条件
        if (queryChronicDiseaseCertificate.getCertificateId() != null) {
            wrapper.eq(ChronicDiseaseCertificate::getCertificateId,
                    queryChronicDiseaseCertificate.getCertificateId());
        }

        if (queryChronicDiseaseCertificate.getFarmerId() != null) {
            wrapper.eq(ChronicDiseaseCertificate::getFarmerId,
                    queryChronicDiseaseCertificate.getFarmerId());
        }

        // 模糊查询 - 疾病名称
        if (StringUtils.isNotBlank(queryChronicDiseaseCertificate.getDiseaseName())) {
            wrapper.like(ChronicDiseaseCertificate::getDiseaseName,
                    queryChronicDiseaseCertificate.getDiseaseName().trim());
        }

        // 时间范围查询 - 确诊/起始时间
        if (queryChronicDiseaseCertificate.getStartTime() != null) {
            wrapper.ge(ChronicDiseaseCertificate::getStartTime,
                    queryChronicDiseaseCertificate.getStartTime());
        }

        // 时间范围查询 - 证书有效期截止时间
        if (queryChronicDiseaseCertificate.getEndTime() != null) {
            wrapper.le(ChronicDiseaseCertificate::getEndTime,
                    queryChronicDiseaseCertificate.getEndTime());
        }

        // 设置分页参数
        Page<ChronicDiseaseCertificate> page = new Page<>(current, size);

        Page<ChronicDiseaseCertificate> resultPage = this.page(page, wrapper);
        fillFarmerName(resultPage.getRecords());
        return resultPage;
    }

    private void fillFarmerName(List<ChronicDiseaseCertificate> certificates) {
        for (ChronicDiseaseCertificate certificate : certificates) {
            if (certificate.getFarmerId() != null) {
                Farmer farmer = farmerMapper.selectById(certificate.getFarmerId());
                if (farmer != null) {
                    certificate.setFarmerName(farmer.getName());
                }
            }
        }
    }

    @Override
    public long getPolicyCount() {
        return this.baseMapper.getPolicyCount();
    }
}
